package com.sgb.kjwl.view.ui.activity.self.lease;

import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLeaseMemberPriceAddBinding;
import com.sgb.kjwl.databinding.LayoutLeaseVipPriceSetBinding;
import com.sgb.kjwl.model.entity.self.LeaseMemberDetailEntity;
import com.sgb.kjwl.utils.EditInputFilter;
import com.sgb.kjwl.viewmodel.self.publish.LeaseMemberPriceAddModel;
import com.sgb.link.event.LeaseEvent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.event.EventConstant;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

/**
 * 租赁会员价格编辑界面
 */
public class LeaseMemberPriceAddActivity extends AppActivity {

    private ActivityLeaseMemberPriceAddBinding binding;
    private String no = "";
    private String type = "";
    private LeaseMemberPriceAddModel viewModel;
    private LeaseMemberDetailEntity bean;
    private String driverText = "(含驾驶员)";
    private List<LayoutLeaseVipPriceSetBinding> viewList = new ArrayList<>();
    private int viewIndex = 0;
    private String marketPrice = "0";//市场价

    public static void start(Context context, String no, String type) {
        Intent intent = new Intent();
        intent.putExtra("no", no);
        intent.putExtra("type", type);
        intent.setClass(context, LeaseMemberPriceAddActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_lease_member_price_add);
        binding.setLifecycleOwner(this);
        viewModel = new ViewModelProvider(this).get(LeaseMemberPriceAddModel.class);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.tvCancel.setOnClickListener(view -> finish());
        binding.include.titleView.setText("新增会员报价");
        binding.tvSubmit.setOnClickListener(v -> toSubmit());
    }

    @Override
    protected void init() {
        //设置第一个会员价格的图片
        binding.includeMember0.rightLayout.setImageResource(R.mipmap.ic_arrow_down_gary);
        //初始化会员价
        binding.itemTvReprice.setContentTextEnd();
        initLabelVisibility(binding.includeMember0, 0);
        initLabelVisibility(binding.includeMember1, 1);
        initLabelVisibility(binding.includeMember2, 2);
        initLabelVisibility(binding.includeMember3, 3);
        initLabelVisibility(binding.includeMember4, 4);
        initLabelVisibility(binding.includeMember5, 5);

        viewModel.showDetailLiveData.observe(this, t -> showDetail(t));
        viewModel.msgLiveData.observe(this, data -> {
            MToast.showToast(getActivity(), data);
            EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_LEASE_MEMBER_LIST_REFRESH));
            EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_LEASE_MEMBER_PRICE_REFRESH));
            finish();
        });
        initExtra();
        viewModel.getMemberDetail(type, no);
    }

    private void initExtra() {
        if (getIntent() != null) {
            type = getIntent().getStringExtra("type");
            no = getIntent().getStringExtra("no");
        }
    }

    private void showDetail(LeaseMemberDetailEntity entity) {
        bean = entity;
        binding.itemTvName.setContent(entity.getTitle());
        binding.itemTvLeaseType.setContent(entity.getLeaseType());
        binding.itvBrand.setContent(entity.getBrand());
        binding.itvModel.setContent(entity.getModel());
//        if ("0".equals(type)) {//机械
//            binding.itemTvType.setLabelName("机械类型");
//        } else if ("1".equals(type)) {//设备
//            binding.itemTvType.setLabelName("设备类型");
//        } else if ("2".equals(type)) {//车辆
//            binding.itemTvType.setLabelName("车辆车型");
//            binding.itvModel.setLabelName("车系");
//        } else if ("3".equals(type)) {//房产价格单位
//            binding.itemTvType.setLabelName("房产类型");
//            binding.itvBrand.setVisibility(View.GONE);
//            binding.itvModel.setVisibility(View.GONE);
//            binding.itvAcreage.setVisibility(View.VISIBLE);
//        } else if ("4".equals(type)) {//土地价格单位
//            binding.itemTvType.setLabelName("土地类型");
//            binding.itvBrand.setVisibility(View.GONE);
//            binding.itvModel.setVisibility(View.GONE);
//            binding.itvAcreage.setVisibility(View.VISIBLE);
//        } else if ("5".equals(type)) {//工厂价格单位
//            binding.itemTvType.setLabelName("工厂类型");
//            binding.itvBrand.setVisibility(View.GONE);
//            binding.itvModel.setVisibility(View.GONE);
//            binding.itvAcreage.setVisibility(View.VISIBLE);
//        } else if ("6".equals(type)) {//农机
//            binding.itemTvType.setLabelName("农机类型");
//        }
        //设置价格数据
        if (bean.getPriceList() != null && bean.getPriceList().size() > 0) {
            LeaseMemberDetailEntity.PriceListDTO priceListDTO = bean.getPriceList().get(0);
            marketPrice = priceListDTO.getMarketPrice();
            binding.itemTvReprice.setLabelName("市场价" + driverText);
            if ("1".equals(bean.getNeedInvoice())) {//含税
                if (TextUtils.isEmpty(priceListDTO.getMarketPrice())) {
                    binding.itemTvReprice.setContent("-");
                } else {
                    binding.itemTvReprice.setContent("租赁价" + priceListDTO.getMarketPrice() + ",含税价" + priceListDTO.getMarketReatePrice());
                }
            } else {
                if (TextUtils.isEmpty(priceListDTO.getMarketPrice())) {
                    binding.itemTvReprice.setContent("-");
                } else {
                    binding.itemTvReprice.setContent("租赁价" + priceListDTO.getMarketPrice());
                }
                binding.includeMember0.itemTvMemberRate.setVisibility(View.GONE);
                binding.includeMember1.itemTvMemberRate.setVisibility(View.GONE);
                binding.includeMember2.itemTvMemberRate.setVisibility(View.GONE);
                binding.includeMember3.itemTvMemberRate.setVisibility(View.GONE);
                binding.includeMember4.itemTvMemberRate.setVisibility(View.GONE);
                binding.includeMember5.itemTvMemberRate.setVisibility(View.GONE);
            }
            binding.includeMember0.itemTvMember.setContent(priceListDTO.getOrdinaryPrice());
            binding.includeMember0.itemTvMemberRate.setContent(priceListDTO.getOrdinaryReatePrice());
            if (!TextUtils.isEmpty(priceListDTO.getOrdinaryPrice())) {
                if("1".equals(bean.getNeedInvoice())){//含税
                    binding.includeMember0.contentText.setText("租赁价" + priceListDTO.getOrdinaryPrice() + ",含税价" + priceListDTO.getOrdinaryReatePrice());
                }else{
                    binding.includeMember0.contentText.setText("租赁价" + priceListDTO.getOrdinaryPrice());
                }

            }

            binding.includeMember1.itemTvMember.setContent(priceListDTO.getOneStarPrice());
            binding.includeMember1.itemTvMemberRate.setContent(priceListDTO.getOneStarReatePrice());
            if (!TextUtils.isEmpty(priceListDTO.getOneStarPrice())) {
                if("1".equals(bean.getNeedInvoice())) {//含税
                    binding.includeMember1.contentText.setText("租赁价" + priceListDTO.getOneStarPrice() + ",含税价" + priceListDTO.getOneStarReatePrice());
                }else{
                    binding.includeMember1.contentText.setText("租赁价" + priceListDTO.getOneStarPrice());
                }
            }

            binding.includeMember2.itemTvMember.setContent(priceListDTO.getTwoStarPrice());
            binding.includeMember2.itemTvMemberRate.setContent(priceListDTO.getTwoStarReatePrice());
            if (!TextUtils.isEmpty(priceListDTO.getTwoStarPrice())) {
                if("1".equals(bean.getNeedInvoice())) {//含税
                    binding.includeMember2.contentText.setText("租赁价" + priceListDTO.getTwoStarPrice() + ",含税价" + priceListDTO.getTwoStarReatePrice());
                }else{
                    binding.includeMember2.contentText.setText("租赁价" + priceListDTO.getTwoStarPrice());
                }
            }

            binding.includeMember3.itemTvMember.setContent(priceListDTO.getThreeStarPrice());
            binding.includeMember3.itemTvMemberRate.setContent(priceListDTO.getThreeStarReatePrice());
            if (!TextUtils.isEmpty(priceListDTO.getThreeStarPrice())) {
                if("1".equals(bean.getNeedInvoice())) {//含税
                    binding.includeMember3.contentText.setText("租赁价" + priceListDTO.getThreeStarPrice() + ",含税价" + priceListDTO.getThreeStarReatePrice());
                }else{
                    binding.includeMember3.contentText.setText("租赁价" + priceListDTO.getThreeStarPrice());
                }
            }

            binding.includeMember4.itemTvMember.setContent(priceListDTO.getFourStarPrice());
            binding.includeMember4.itemTvMemberRate.setContent(priceListDTO.getFourStarReatePrice());
            if (!TextUtils.isEmpty(priceListDTO.getFourStarPrice())) {
                if("1".equals(bean.getNeedInvoice())) {//含税
                    binding.includeMember4.contentText.setText("租赁价" + priceListDTO.getFourStarPrice() + ",含税价" + priceListDTO.getFourStarReatePrice());
                }else {
                    binding.includeMember4.contentText.setText("租赁价" + priceListDTO.getFourStarPrice());
                }
            }

            binding.includeMember5.itemTvMember.setContent(priceListDTO.getFiveStarPrice());
            binding.includeMember5.itemTvMemberRate.setContent(priceListDTO.getFiveStarReatePrice());
            if (!TextUtils.isEmpty(priceListDTO.getFiveStarPrice())) {
                if("1".equals(bean.getNeedInvoice())) {//含税
                    binding.includeMember5.contentText.setText("租赁价" + priceListDTO.getFiveStarPrice() + ",含税价" + priceListDTO.getFiveStarReatePrice());
                }else{
                    binding.includeMember5.contentText.setText("租赁价" + priceListDTO.getFiveStarPrice());
                }
            }

        }
    }

    private void toSubmit() {
        try {
            if (!TextUtils.isEmpty(binding.includeMember0.itemTvMember.getContent().trim()) && Double.parseDouble(binding.includeMember0.itemTvMember.getContent().trim()) <= 0) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "报价金额最小为0.01");
                return;
            }
            if (!TextUtils.isEmpty(binding.includeMember1.itemTvMember.getContent().trim()) && Double.parseDouble(binding.includeMember1.itemTvMember.getContent().trim()) <= 0) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "报价金额最小为0.01");
                return;
            }
            if (!TextUtils.isEmpty(binding.includeMember2.itemTvMember.getContent().trim()) && Double.parseDouble(binding.includeMember2.itemTvMember.getContent().trim()) <= 0) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "报价金额最小为0.01");
                return;
            }
            if (!TextUtils.isEmpty(binding.includeMember3.itemTvMember.getContent().trim()) && Double.parseDouble(binding.includeMember3.itemTvMember.getContent().trim()) <= 0) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "报价金额最小为0.01");
                return;
            }
            if (!TextUtils.isEmpty(binding.includeMember4.itemTvMember.getContent().trim()) && Double.parseDouble(binding.includeMember4.itemTvMember.getContent().trim()) <= 0) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "报价金额最小为0.01");
                return;
            }
            if (!TextUtils.isEmpty(binding.includeMember5.itemTvMember.getContent().trim()) && Double.parseDouble(binding.includeMember5.itemTvMember.getContent().trim()) <= 0) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "报价金额最小为0.01");
                return;
            }
        } catch (Exception e) {

        }

        //判断会员价不能大于市场价
        try {
            if (Double.parseDouble(binding.includeMember0.itemTvMember.getContent()) >= Double.parseDouble(marketPrice)) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "普通会员价不能大于等于市场价");
                return;
            }
            if (Double.parseDouble(binding.includeMember1.itemTvMember.getContent()) >= Double.parseDouble(marketPrice)) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "一级会员价不能大于等于市场价");
                return;
            }
            if (Double.parseDouble(binding.includeMember2.itemTvMember.getContent()) >= Double.parseDouble(marketPrice)) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "二级会员价不能大于等于市场价");
                return;
            }
            if (Double.parseDouble(binding.includeMember3.itemTvMember.getContent()) >= Double.parseDouble(marketPrice)) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "三级会员价不能大于等于市场价");
                return;
            }
            if (Double.parseDouble(binding.includeMember4.itemTvMember.getContent()) >= Double.parseDouble(marketPrice)) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "四级会员价不能大于等于市场价");
                return;
            }
            if (Double.parseDouble(binding.includeMember5.itemTvMember.getContent()) >= Double.parseDouble(marketPrice)) {
                MToast.showToast(LeaseMemberPriceAddActivity.this, "五级会员价不能大于等于市场价");
                return;
            }
        } catch (Exception ex) {

        }

        LeaseMemberDetailEntity.PriceListDTO priceListDTO = new LeaseMemberDetailEntity.PriceListDTO();
        if (bean.getPriceList() != null && bean.getPriceList().size() > 0) {
            priceListDTO = bean.getPriceList().get(0);
        }

        //普通会员
        priceListDTO.setOrdinaryPrice(binding.includeMember0.itemTvMember.getContent());
        priceListDTO.setOrdinaryReatePrice(binding.includeMember0.itemTvMemberRate.getContent());
        //一星会员
        priceListDTO.setOneStarPrice(binding.includeMember1.itemTvMember.getContent());
        priceListDTO.setOneStarReatePrice(binding.includeMember1.itemTvMemberRate.getContent());

        priceListDTO.setTwoStarPrice(binding.includeMember2.itemTvMember.getContent());
        priceListDTO.setTwoStarReatePrice(binding.includeMember2.itemTvMemberRate.getContent());

        priceListDTO.setThreeStarPrice(binding.includeMember3.itemTvMember.getContent());
        priceListDTO.setThreeStarReatePrice(binding.includeMember3.itemTvMemberRate.getContent());

        priceListDTO.setFourStarPrice(binding.includeMember4.itemTvMember.getContent());
        priceListDTO.setFourStarReatePrice(binding.includeMember4.itemTvMemberRate.getContent());

        priceListDTO.setFiveStarPrice(binding.includeMember5.itemTvMember.getContent());
        priceListDTO.setFiveStarReatePrice(binding.includeMember5.itemTvMemberRate.getContent());

        bean.getPriceList().set(0, priceListDTO);

        bean.setLeaseNo(no);
        viewModel.addMember(bean);
    }

    /**
     * 设置隐藏显示
     */
    private void initLabelVisibility(LayoutLeaseVipPriceSetBinding vipPriceSetBinding, int i) {
        viewList.add(vipPriceSetBinding);
        if (i == 0) {
            vipPriceSetBinding.labelNameTv.setText("普通会员" + driverText);
            vipPriceSetBinding.llMember.setVisibility(View.VISIBLE);
        } else if (i == 1) {
            vipPriceSetBinding.labelNameTv.setText("一级会员" + driverText);
        } else if (i == 2) {
            vipPriceSetBinding.labelNameTv.setText("二级会员" + driverText);
        } else if (i == 3) {
            vipPriceSetBinding.labelNameTv.setText("三级会员" + driverText);
        } else if (i == 4) {
            vipPriceSetBinding.labelNameTv.setText("四级会员" + driverText);
        } else if (i == 5) {
            vipPriceSetBinding.labelNameTv.setText("五级会员" + driverText);
        }
        vipPriceSetBinding.itemTvMember.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        vipPriceSetBinding.itemTvMember.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        //收缩和展示
        vipPriceSetBinding.rvLabelGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibilityByRateText(viewList.get(viewIndex));//关闭之前的状态
                if (vipPriceSetBinding.llMember.getVisibility() == View.GONE) {
                    vipPriceSetBinding.llMember.setVisibility(View.VISIBLE);
                    vipPriceSetBinding.rightLayout.setImageResource(R.mipmap.ic_arrow_down_gary);
                    viewIndex = i;//保持标识
                } else {
                    vipPriceSetBinding.llMember.setVisibility(View.GONE);
                    vipPriceSetBinding.rightLayout.setImageResource(R.mipmap.right_arrow_gray);
                    if (!TextUtils.isEmpty(vipPriceSetBinding.itemTvMember.getContent().trim())) {
                        if("1".equals(bean.getNeedInvoice())) {//含税
                            vipPriceSetBinding.contentText.setText("租赁价" + vipPriceSetBinding.itemTvMember.getContent() + ",含税价" + vipPriceSetBinding.itemTvMemberRate.getContent());
                        }else{
                            vipPriceSetBinding.contentText.setText("租赁价" + vipPriceSetBinding.itemTvMember.getContent());
                        }
                    } else {
                        vipPriceSetBinding.contentText.setText("");
                    }
                }

            }
        });

        vipPriceSetBinding.itemTvMember.onTextChange(new ItemInputTextView.onTextChange() {
            @Override
            public void change(String s) {
                try {
                    if (TextUtils.isEmpty(s.trim())) {
                        vipPriceSetBinding.itemTvMemberRate.setContent("");
                    } else if(!TextUtils.isEmpty(bean.getTariff())){
                        double ratePrice = Double.parseDouble(s) + Double.parseDouble(s) * Double.parseDouble(bean.getTariff()) / 100;
                        vipPriceSetBinding.itemTvMemberRate.setContent(toFormat(ratePrice));
                    }
                } catch (Exception ex) {

                }
            }
        });
    }

    private void setVisibilityByRateText(LayoutLeaseVipPriceSetBinding vipPriceSetBinding) {
        vipPriceSetBinding.llMember.setVisibility(View.GONE);
        vipPriceSetBinding.rightLayout.setImageResource(R.mipmap.right_arrow_gray);
        if (!TextUtils.isEmpty(vipPriceSetBinding.itemTvMember.getContent().trim())) {
            if("1".equals(bean.getNeedInvoice())) {//含税
                vipPriceSetBinding.contentText.setText("租赁价" + vipPriceSetBinding.itemTvMember.getContent() + ",含税价" + vipPriceSetBinding.itemTvMemberRate.getContent());
            }else{
                vipPriceSetBinding.contentText.setText("租赁价" + vipPriceSetBinding.itemTvMember.getContent());
            }
        } else {
            vipPriceSetBinding.contentText.setText("");
        }
    }

    private String toFormat(double d) {
        return new DecimalFormat("#0.00").format(d);
    }

}
